package org.desktop.web.client.project.grid;

import java.util.ArrayList;
import java.util.List;

import org.desktop.web.client.project.proxy.RpcProjectProxy;
import org.desktop.web.shared.domain.entities.Project;
import org.desktop.web.shared.domain.project.bean.ProjectProperties;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.loader.FilterPagingLoadConfig;
import com.sencha.gxt.data.shared.loader.FilterPagingLoadConfigBean;
import com.sencha.gxt.data.shared.loader.LoadResultListStoreBinding;
import com.sencha.gxt.data.shared.loader.PagingLoadResult;
import com.sencha.gxt.data.shared.loader.PagingLoader;
import com.sencha.gxt.widget.core.client.event.RowClickEvent;
import com.sencha.gxt.widget.core.client.event.RowClickEvent.RowClickHandler;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.grid.Grid;
import com.sencha.gxt.widget.core.client.grid.filters.GridFilters;
import com.sencha.gxt.widget.core.client.grid.filters.StringFilter;
import com.sencha.gxt.widget.core.client.toolbar.PagingToolBar;

public class ProjectGrid implements IsWidget {
	// private ListStore<Project> store;

	private ProjectProperties properties = GWT.create(ProjectProperties.class);
	private SubProjectsGrid child;
	private Grid<Project> grid;

	private  PagingLoader<FilterPagingLoadConfig, PagingLoadResult<Project>> loader;
	private PagingToolBar pagingToolBar;

	public PagingToolBar getPagingToolBar() {
		return pagingToolBar;
	}

	public ProjectGrid() {
		super();
		// TODO Auto-generated constructor stub
	}

	public ProjectGrid(SubProjectsGrid child) {
		super();
		this.child = child;
	}

	@Override
	public Widget asWidget() {

		// ProjectAutoBeanFactory factory = GWT
		// .create(ProjectAutoBeanFactory.class);
		//
		// ProjectDataRecordJsonReader reader = new
		// ProjectDataRecordJsonReader(factory,
		// ProjectRecordResult.class);

		// String path = com.google.gwt.core.client.GWT.getModuleBaseURL()
		// + "exec?service=" + WebService.GET_PROJECT_LIST_ADMIN;
		// RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
		// path);
		// builder.setHeader("token", Cookies.getCookie("token"));
		//
		//
		// //TODO
		// //sostituire con PagingLoadConfig e PagingLoadResult
		// HttpProxy<ListLoadConfig> proxy = new
		// HttpProxy<ListLoadConfig>(builder);

		RpcProjectProxy proxy = new RpcProjectProxy();
		loader = new PagingLoader<FilterPagingLoadConfig, PagingLoadResult<Project>>(
				proxy) {

			@Override
			protected FilterPagingLoadConfig newLoadConfig() {
				// TODO Auto-generated method stub
				 FilterPagingLoadConfigBean loadConfigBean = new FilterPagingLoadConfigBean();
//				 loadConfigBean.setLimit(2);
//				 loadConfigBean.setOffset(1);
				return loadConfigBean;
			}

		};
		// final ListLoader<ListLoadConfig, ListLoadResult<Project>> loader =
		// new ListLoader<ListLoadConfig, ListLoadResult<Project>>(
		// proxy, reader);
		// loader.useLoadConfig(factory.create(ListLoadConfig.class).as());

		final ListStore<Project> store = new ListStore<Project>(
				properties.key());
		
		// loader.addLoadHandler(new LoadResultListStoreBinding<ListLoadConfig,
		// Project, ListLoadResult<Project>>(
		// store));
		loader.setRemoteSort(true);
		loader.addLoadHandler(new LoadResultListStoreBinding<FilterPagingLoadConfig, Project, PagingLoadResult<Project>>(
				store));
		

		 pagingToolBar = new PagingToolBar(2);
		pagingToolBar.bind(loader);
		
		
		
		ColumnConfig<Project, String> columnDescProject = new ColumnConfig<>(
				properties.descProject(), 100, "Nome progetto");
		// ColumnConfig<Project, String> columnAddUsers = new ColumnConfig<>(
		// properties.key(), 100, "Add users");
		// TextButtonCell buttonAddUsers = new TextButtonCell();
		// buttonAddUsers.setIcon(GWT.<Resources>create(Resources.class).add());
		//
		// buttonAddUsers.addSelectHandler(new SelectHandler() {
		//
		// @Override
		// public void onSelect(SelectEvent event) {
		// Context c = event.getContext();
		// int row = c.getIndex();
		// Project p = store.get(row);
		// new ModUserWindow(p).show();
		// // Info.display("Event", "The " + p.getName() +
		// // " was clicked.");
		// }
		// });
		// columnAddUsers.setCell(buttonAddUsers);

		List<ColumnConfig<Project, ?>> l = new ArrayList<ColumnConfig<Project, ?>>();
		l.add(columnDescProject);
		// l.add(columnAddUsers);
		ColumnModel<Project> cm = new ColumnModel<Project>(l);
		
		
		StringFilter<Project> projectNameFilter = new StringFilter<Project>(properties.descProject());
		
		

		grid = new Grid<Project>(store, cm);
		if (this.child != null) {
			final SubProjectsGrid ch = this.child;
			grid.addRowClickHandler(new RowClickHandler() {

				@Override
				public void onRowClick(RowClickEvent event) {
					ch.load(store.get(event.getRowIndex()).getId());
				}
			});
		}
		grid.getView().setForceFit(true);
		grid.setLoader(loader);
		load();
		grid.setBorders(true);
		
		
		GridFilters<Project> filters = new GridFilters<Project>(loader);
		filters.initPlugin(grid);
		filters.addFilter(projectNameFilter);
		
		
		// TODO Auto-generated method stub
		return grid;
	}

	public Grid<Project> getGrid() {
		return grid;
	}

	public void load() {
		grid.setLoadMask(true);
		loader.load();

	}

}
